package cn.iocoder.yudao.module.alipay.controller.app.alipaypermission;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.alipay.service.alipermission.AliPayService;
import com.alipay.api.AlipayApiException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "支付宝相关 - 权限以及支付")
@RestController
@RequestMapping("/alipay/mini-program/")
@Validated
public class AliPayController {


    @Resource
    private AliPayService aliPayService;


    /**
     * (支付宝) 换取授权访问令牌
     * @param grantType 必选 授权方式
     * @param code 可选 授权码
     * @param refreshToken 可选 刷新令牌
     * @return
     */
    @GetMapping("/getAuthToken")
    @Operation(summary = "获取用户授权访问令牌")
    public CommonResult<Map> getAuthToken(@RequestParam("grantType")String grantType,@RequestParam("code")String code, String refreshToken,@RequestParam("appId") String appId) throws AlipayApiException {
        return success(aliPayService.getAuthToken(grantType,code,refreshToken,appId));
    }

    /**
     * (支付宝) 换取授权访问令牌
     * @return
     */
    @GetMapping("/createOrder")
    @Operation(summary = "创建订单")
    public CommonResult<Object> createOrder(@RequestParam("buyerId")String buyerId,@RequestParam("totalAmount") BigDecimal totalAmount,@RequestParam("outTradeNo")String outTradeNo,@RequestParam("appId")String appId) throws AlipayApiException {
        return success(aliPayService.createOrder(buyerId,totalAmount,outTradeNo,appId));
    }


    /**
     * (支付宝) 成功回调
     * @return
     */
    @RequestMapping("/successNotification")
    @Operation(summary = "成功回调")
    public CommonResult<Object> successNotification(HttpServletResponse response, HttpServletRequest request) throws AlipayApiException {
        return success(aliPayService.successNotification(response,request));
    }

    /**
     * (支付宝) 授权回调
     * @return
     */
    @RequestMapping("/aliAuthNotification")
    @Operation(summary = "授权回调")
    public CommonResult<Object> aliAuthNotification(String appAuthCode,String appId) throws AlipayApiException {
        return success(aliPayService.aliAuthNotification(appAuthCode,appId));
    }



}
